<?php

/**
 * esta basado en la clase Zend_Validate_Db_NoRecordExists solo por que la use como ejemplo
 * uso el campo exclude para otra cosa.
 */
class Ext_Validate_Lov extends Zend_Validate_Db_NoRecordExists {


    /**
     * Run query and returns matches, or null if no matches are found.
     *
     * @param  String $value
     * @return Array when matches are found.
     */
    protected function _query($value)
    {
        /**
         * Check for an adapter being defined. if not, fetch the default adapter.
         */
        if ($this->_adapter === null) {
            $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
            if (null === $this->_adapter) {
                require_once 'Zend/Validate/Exception.php';
                throw new Zend_Validate_Exception('No database adapter present');
            }
        }

        /**
         * Build select object
         */
        $select = new Zend_Db_Select($this->_adapter);
        $select->from($this->_table, array($this->_field))
               ->where($this->_adapter->quoteIdentifier($this->_field).' = ?', $value);
        if ($this->_exclude !== null) {
            if (is_array($this->_exclude)) {
                // OBS: Aca cambie un != por un = !!!
                $select->where($this->_adapter->quoteIdentifier($this->_exclude['field']).' = ?', $this->_exclude['value']);
            } else {
                $select->where($this->_exclude);
            }
        }
        $select->limit(1);
        /**
         * Run query
         */
        $result = $this->_adapter->fetchRow($select, array(), Zend_Db::FETCH_ASSOC);

        return $result;
    }
}
